package com.leiduoduo.oms.controller;

import com.leiduoduo.oms.annotation.PassToken;
import com.leiduoduo.oms.pojo.Restaurant;
import com.leiduoduo.oms.pojo.Token;
import com.leiduoduo.oms.service.RestaurantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@CrossOrigin
public class RestaurantController {
    @Autowired
    RestaurantService restaurantService;

    @PostMapping("/addRestaurant")
    public boolean addRestaurant(Restaurant restaurant){
        return restaurantService.addRestaurant(restaurant);
    }
    @PostMapping("/delRestaurant")
    public boolean delRestaurant(Restaurant restaurant){
        return restaurantService.delRestaurant(restaurant);
    }
    @GetMapping("/upRestaurant")
    public boolean upRestaurant(Restaurant restaurant){
        return restaurantService.upRestaurant(restaurant);
    }

    /**
     * 获取当前餐馆信息
     * @param code 餐馆编码
     * @return
     */
    @GetMapping("/getRestaurantByCode")
    @PassToken
    public Map<String, Object> getRestaurantById(String code){
        Map<String, Object> result = new HashMap<>();
        Restaurant restaurant = restaurantService.getRestaurantByCode(code);
        if (restaurant != null){
            result.put("data",restaurant);
            result.put("msg","success");
            result.put("errorcode",0);
        } else {
            result.put("data",null);
            result.put("msg","code不存在");
            result.put("errorcode",422);
        }
        return result;
    }

    /**
     * 获取自动接单状态
     * @param code 餐馆编码
     * @return
     */
    @GetMapping("/getIfAutoById")
    @PassToken
    public Map<String, Object> getIfAutoById(String code){
        Map<String, Object> result = new HashMap<>();
        Restaurant restaurant = restaurantService.getRestaurantByCode(code);
        if (restaurant != null){
            result.put("data",restaurant.getAuto_order_receiving());
            result.put("msg","success");
            result.put("errorcode",0);
        } else {
            result.put("data",null);
            result.put("msg","code不存在");
            result.put("errorcode",422);
        }
        return result;
    }
    @GetMapping("/getRestaurantList")
    @PassToken
    public List<Restaurant> getRestaurantList(){
        return restaurantService.getRestaurantList();
    }

    /**
     * 登录获取token
     * @param code 餐馆编码
     * @param password 餐馆密码
     * @return
     * @throws Exception
     */
    @PostMapping("/restaurantLogin")
    @PassToken
    public Map<String, Object> restaurantLogin(String code, String password) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        Token token = new Token();
        Restaurant restaurant = restaurantService.getRestaurantByCode(code);
        log.info(restaurant.toString());
        if (restaurant != null){
            if (restaurant.getPassword().equals(password)) {
                restaurant.setToken(token.getToken(restaurant.getCode()));
                restaurantService.upRestaurant(restaurant);
                result.put("data",restaurant);
                result.put("msg","登录成功");
                result.put("errorcode",0);
            } else {
                result.put("data",null);
                result.put("msg","登录失败");
                result.put("errorcode",422);
            }
        } else {
            result.put("data",null);
            result.put("msg","登录失败");
            result.put("errorcode",422);
        }

        return result;
    }

    /**
     * 设置自动接单状态
     * @param code 餐馆编码
     * @return
     */
    @PostMapping("/setIfAutoById")
    @PassToken
    public Map<String, Object> setIfAutoById(String code){
        Map<String, Object> result = new HashMap<>();
        Restaurant restaurant = restaurantService.getRestaurantByCode(code);
        if (restaurant != null){
            restaurant.setAuto_order_receiving((byte) (restaurant.getAuto_order_receiving()==0?1:0));
            restaurantService.upRestaurantAuto(restaurant);
            result.put("data",restaurant.getAuto_order_receiving());
            result.put("msg","success");
            result.put("errorcode",0);
        } else {
            result.put("data",null);
            result.put("msg","code不存在");
            result.put("errorcode",422);
        }
        return result;
    }
}
